#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import time


base_dir = r'D:\Workspace\Python'
filelists = []
# 指定想要统计的文件类型
whitelist = ['py']
# 遍历文件, 递归遍历文件夹中的所有


def get_file(basedir):
    global filelists
    for parent, dirnames, filenames in os.walk(basedir):
        # for dirname in dirnames:
        # getFile(os.path.join(parent,dirname)) #递归
        for filename in filenames:
            ext = filename.split('.')[-1]
            # 只统计指定的文件类型，略过一些log和cache文件
            if ext in whitelist:
                filelists.append(os.path.join(parent, filename))
                

# 统计一个文件的行数
def count_line(fname):
    count = 0
    for file_line in open(fname, encoding='utf-8').readlines():
        if file_line != '' and file_line != '\n': # 过滤掉空行
            count += 1
    print(fname + '----', count)
    return count


if __name__ == '__main__':
    startTime = time.clock()
    get_file(base_dir)
    totalline = 0
    for filelist in filelists:
        totalline = totalline + count_line(filelist)
    print('total lines:', totalline)
    print('Done! Cost Time: %0.2f second' % (time.clock() - startTime))